using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;
using System.Text;

namespace BlazeApps.Library.Form
{
    [Serializable()]
    public class FormQuestionColumsDetail : Csla.BusinessBase<FormQuestionColumsDetail>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _formQuestionID = Guid.NewGuid();
        private Guid _formColumnID = Guid.NewGuid();
        private string _columnChoice = string.Empty;
        private SmartDate _createDate = new SmartDate(false);
        private int _sortOrder = 0;

        public Guid FormQuestionID
        {
            get
            {
                CanReadProperty("FormQuestionID", true);
                return _formQuestionID;
            }
            set
            {
                CanWriteProperty("FormQuestionID", true);
                if (!_formQuestionID.Equals(value))
                {
                    _formQuestionID = value;
                    PropertyHasChanged("FormQuestionID");
                }
            }
        }

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid FormColumnID
        {
            get
            {
                CanReadProperty("FormColumnID", true);
                return _formColumnID;
            }
        }

        public string ColumnChoice
        {
            get
            {
                CanReadProperty("ColumnChoice", true);
                return _columnChoice;
            }
            set
            {
                CanWriteProperty("ColumnChoice", true);
                if (value == null)
                    value = string.Empty;
                if (!_columnChoice.Equals(value))
                {
                    _columnChoice = value;
                    PropertyHasChanged("ColumnChoice");
                }
            }
        }

        public DateTime CreateDate
        {
            get
            {
                CanReadProperty("CreateDate", true);
                return _createDate.Date;
            }
        }

        public string CreateDateString
        {
            get
            {
                CanReadProperty("CreateDate", true);
                return _createDate.Text;
            }
            set
            {
                CanWriteProperty("CreateDate", true);
                if (value == null)
                    value = string.Empty;
                if (!_createDate.Equals(value))
                {
                    _createDate.Text = value;
                    PropertyHasChanged("CreateDate");
                }
            }
        }

        public int SortOrder
        {
            get
            {
                CanReadProperty("SortOrder", true);
                return _sortOrder;
            }
            set
            {
                CanWriteProperty("SortOrder", true);
                if (!_sortOrder.Equals(value))
                {
                    _sortOrder = value;
                    PropertyHasChanged("SortOrder");
                }
            }
        }

        protected override object GetIdValue()
        {
            return _formColumnID;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
            //
            // ColumnChoice
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "ColumnChoice");
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in FormQuestionColumsDetail
            //AuthorizationRules.AllowRead("FormQuestionID", "FormQuestionColumsDetailReadGroup");
            //AuthorizationRules.AllowRead("FormColumnID", "FormQuestionColumsDetailReadGroup");
            //AuthorizationRules.AllowRead("ColumnChoice", "FormQuestionColumsDetailReadGroup");

            //AuthorizationRules.AllowWrite("ColumnChoice", "FormQuestionColumsDetailWriteGroup");
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in FormQuestionColumsDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormQuestionColumsDetailViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in FormQuestionColumsDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormQuestionColumsDetailAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in FormQuestionColumsDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormQuestionColumsDetailEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in FormQuestionColumsDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormQuestionColumsDetailDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private FormQuestionColumsDetail()
        { /* require use of factory method */
        }

        public static FormQuestionColumsDetail NewFormQuestionColumsDetail()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a FormQuestionColumsDetail");
            return DataPortal.Create<FormQuestionColumsDetail>();
        }

        public static FormQuestionColumsDetail GetFormQuestionColumsDetail(Guid formColumnID)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a FormQuestionColumsDetail");
            return DataPortal.Fetch<FormQuestionColumsDetail>(new Criteria(formColumnID));
        }

        public static void DeleteFormQuestionColumsDetail(Guid formColumnID)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a FormQuestionColumsDetail");
            DataPortal.Delete(new Criteria(formColumnID));
        }

        public override FormQuestionColumsDetail Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a FormQuestionColumsDetail");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a FormQuestionColumsDetail");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a FormQuestionColumsDetail");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid FormColumnID;

            public Criteria(Guid formColumnID)
            {
                this.FormColumnID = formColumnID;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {

            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@FormColumnID", criteria.FormColumnID);

            using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetFormQuestionColumnsByQuestionID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            dr.Read();
            _formQuestionID = dr.GetGuid("FormQuestionID");
            _formColumnID = dr.GetGuid("FormColumnID");
            _columnChoice = dr.GetString("ColumnChoice");
            _createDate = dr.GetSmartDate("CreateDate", _createDate.EmptyIsMin);
            _sortOrder = dr.GetInt32("SortOrder");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert

        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertFormQuestionColumns.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@ColumnChoice", _columnChoice);
            DAL.AddParameter("@FormQuestionID", _formQuestionID);
            DAL.AddParameter("@FormColumnID", _formColumnID);
            DAL.AddParameter("@CreateDate", _createDate.DBValue);
        }
        #endregion //Data Access - Insert

        #region Data Access - Update

        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdateFormQuestionColumns.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@FormQuestionID", _formQuestionID);
            DAL.AddParameter("@FormColumnID", _formColumnID);
            DAL.AddParameter("@ColumnChoice", _columnChoice);
            DAL.AddParameter("@SortOrder", _sortOrder);
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_formColumnID));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);

            DAL.ExecuteNonQuery(Procs.DeleteFormQuestionColumns.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@FormColumnID", DbType.Guid, criteria.FormColumnID);
        }

        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}
